// pages/orderEvaluate/orderEvaluate.js
var core = require("../../utils/core.js");
import Notify from '../../components/dist/notify/notify';
import Toast from '../../components/dist/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputText: '', //输入的文字
    fileList: [],
    id: '',
    ImgUrlList: [], //上传图片url数组
    orderid: '',
    goodid: '',
    goodsInfo: [], //商品信息
    orderInfo: '', //订单信息

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      orderid: options.id,
      goodid: options.goodid,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getInfo();
  },
  getInfo: function() {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    core.get("groupapp.comment", {
      id: that.data.orderid, //orderid
      goodsid: that.data.goodid
    }, function(t) {

      if (t.error == 0) {
        that.setData({
          goodsInfo: t.goods,
          orderInfo: t.order
        })
      } else {

      }

      wx.hideLoading()
    })
  },
  textareaChange: function(e) {
    this.setData({
      inputText: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  submit: function() { //提交评价
    var that = this;
    var t = {
      orderid: this.data.orderid,
      comments: []
    };
    if (this.data.inputText == '') {
      Toast('评论为空');
      return
    }
    var i = {
      goodsid: this.data.goodid,
      level: 5,
      content: this.data.inputText,
      images: this.data.ImgUrlList
    };
    t.comments.push(i);


    wx.showLoading({
      title: '提交中',
    })
    core.get("groupapp.comment.submit", t, function(t) {

      if (t.error == 0) {
        Toast.success('提交成功');
      } else {
        Toast.fail('提交失败');
      }

      wx.hideLoading()

    })










  },
  onChange(e) {

    const {
      file
    } = e.detail
    if (file.status === 'uploading') {
      this.setData({
        progress: 0,
      })
      wx.showLoading({
        title: '上传中...'
      });

    } else if (file.status === 'done') {
      this.setData({
        imageUrl: file.url,
      })
    }
  },
  onSuccess(e) {
    var arr = this.data.ImgUrlList;
    var str = e.detail.file.res.data;
    var obj = JSON.parse(e.detail.file.res.data)
    var url = obj.files[0].url;
    arr.push(url);
    this.setData({
      ImgUrlList: arr
    })
  },
  onFail(e) {

  },
  onComplete(e) {

    wx.hideLoading()
  },
  onProgress(e) {

    this.setData({
      progress: e.detail.file.progress,
    })
  },
  onPreview(e) {
    console.log(e);
    var uid = e.detail.file.uid;
    // for(var i=0;i<){

    // }
    // const {
    //   file,
    //   fileList
    // } = e.detail
    // wx.previewImage({
    //   current: file.url,
    //   urls: fileList.map((n) => n.url),
    // })
  },
  onRemove(e) {
    const {
      file,
      fileList
    } = e.detail
    wx.showModal({
      content: '确定删除？',
      success: (res) => {
        if (res.confirm) {
          this.setData({
            fileList: fileList.filter((n) => n.uid !== file.uid),
          })
        }
      },
    })
  },
})